package com.feeyo.groundservice.checkin.base

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.blankj.utilcode.util.Utils
import com.feeyo.groundservice.base_util.getData
import com.feeyo.groundservice.base_util.putData

/**
 *作者：QLH on 2023-10-11
 *描述：
 */
internal object AppDataStore {

    // 创建DataStore
    private val Context.appDataStore: DataStore<Preferences> by preferencesDataStore(
        name = "App"
    )

    // DataStore变量
    private val dataStore = Utils.getApp().appDataStore

    //------------------------------------对外-------------------------------

    fun saveCheckInIsland(checkInIsland: String) {
        putData(DataStoreKey.checkInIsland, checkInIsland)
    }

    fun getCheckInIsland(): String {
        return getData(DataStoreKey.checkInIsland, "")
    }

    fun clear() {
        dataStore.putData(DataStoreKey.checkInIsland, "")
    }

    //----------------------------------内部--------- T 只能为基本数据类型-------------------------

    private fun <T> putData(key: String, value: T) {
        dataStore.putData(key, value)
    }

    private fun <T> getData(key: String, defaultValue: T): T {
        return dataStore.getData(key, defaultValue)
    }

    private object DataStoreKey {
       const val checkInIsland = "checkInIsland"
    }

}